package com.zhuiyun.project.api.joingroup.mapper;

import com.zhuiyun.project.api.group.entity.Group;
import com.zhuiyun.project.api.joingroup.entity.JoinGroup;
import com.zhuiyun.project.api.joingroup.model.JoinGroupModel;
import com.zhuiyun.project.common.mapper.BaseMapper;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName JoinGroupMapper
 * @Description TODO
 * @Author kdj
 * @Date 2023/7/24 17:18
 **/
@Mapper
public interface JoinGroupMapper extends BaseMapper<JoinGroup> {
    /**
     * @param userId
     * @param groupId
     * @param type
     * @Author kdj
     * @Description 根据userId，groupId，type 获取JoinGroup数据
     * @Date 2023/7/24 21:27
     * @Return com.zhuiyun.project.api.joingroup.entity.JoinGroup
     */
    @Select("select * from join_group where user_id=#{userId} and group_id=#{groupId} and user_type=#{type} and del_flag=0 ")
    JoinGroup getGroupData(int userId, int groupId, int type);

    /**
     * @param id
     * @param type
     * @Author kdj
     * @Description 根据用户id和类型获取用户加入的群聊id
     * @Date 2023/7/25 16:15
     * @Return java.util.List<java.lang.Integer>
     */
    @Select("select group_id from join_group where user_id=#{id} and user_type=#{type} and del_flag=0 and join_status=1")
    List<Integer> queryGroupsByUserId(int id, int type);

    /**
     * @param id
     * @param type
     * @Author kdj
     * @Description 根据用户id和类型获取用户加入的群聊id
     * @Date 2023/7/25 16:15
     * @Return java.util.List<java.lang.Integer>
     */
    @Select("select * from join_group  LEFT JOIN  group_chat on join_group.group_id = group_chat.id where user_id=#{id} and user_type=#{type} and join_group.del_flag=0 and join_status=1")
    List<Map> queryGroupsDataByUserId(int id, int type);

    /**
     * @param groupId
     * @Author kdj
     * @Description 根据群id获取入群申请
     * @Date 2023/7/27 16:42
     * @Return com.zhuiyun.project.api.joingroup.entity.JoinGroup
     */
    @Select("select jg.*,userImgId,user_name,group_img_id,group_name from join_group jg left JOIN (" +
            "    SELECT" +
            "        id," +
            "        user_name," +
            "        image_id AS userImgId," +
            "        'app_user' AS user_type" +
            "    FROM" +
            "        app_user" +
            "    UNION ALL" +
            "    SELECT" +
            "        id," +
            "        user_name," +
            "        avatar_id AS userImgId," +
            "        'sys_user' AS user_type" +
            "    FROM" +
            "        sys_user" +
            " ) AS user_data ON jg.user_id = user_data.id   " +
            " Left JOIN group_chat  gc on gc.id=jg.group_id" +
            " where gc.id=#{groupId} and jg.user_type=1 order by update_time desc")
    List<JoinGroupModel> getJoinGroupAuditsById(int groupId);

    /**
     * @param userId
     * @Author kdj
     * @Description 根据创建人id获取入群申请
     * @Date 2023/7/27 16:42
     * @Return com.zhuiyun.project.api.joingroup.entity.JoinGroup
     */
    @Select("select jg.*,userImgId,user_name,group_img_id,group_name from join_group jg left JOIN (" +
            "    SELECT" +
            "        id," +
            "        user_name," +
            "        image_id AS userImgId," +
            "        'app_user' AS user_type" +
            "    FROM" +
            "        app_user" +
//            "    UNION ALL" +
//            "    SELECT" +
//            "        id," +
//            "        user_name," +
//            "        avatar_id AS userImgId," +
//            "        'sys_user' AS user_type" +
//            "    FROM" +
//            "        sys_user" +
            " ) AS user_data ON jg.user_id = user_data.id  and jg.user_type=1  " +
            " Left JOIN group_chat  gc on gc.id=jg.group_id" +
            " where gc.create_user_id=#{userId} order by jg.update_time desc")
    List<JoinGroupModel> getJoinGroupAuditsByCreateUserId(int userId);


    /**
     * @param
     * @Author kdj
     * @Description 根据群聊id获取群聊成员
     * @Date 2023/7/26 10:59
     * @Return java.util.List<com.zhuiyun.project.api.joingroup.entity.JoinGroup>
     */

    @Select("SELECT  b.user_name AS userName,b.image_id AS imageId ,m.mushin_time mushinTime,a.join_time joinTime,b.id As userId FROM join_group a LEFT JOIN app_user b ON a.user_id = b.id LEFT JOIN message_list m on m.from_id=  a.group_id and m.to_id= a.user_id where a.group_id = #{groupId}   and a.del_flag = 0 and a.join_status=1 and m.del_flag=0")
    List<Map>  setByGroupId(int groupId);


    /**
     * @param groupId
     * @param userId
     * @Author kdj
     * @Description 根据群号查询群成员
     * @Date 2023/8/10 18:44
     * @Return java.util.List<com.zhuiyun.project.api.joingroup.entity.JoinGroup>
     */
    @Select("SELECT  b.user_name AS userName,b.image_id AS imageId ,m.mushin_time mushinTime,a.join_time joinTime,b.id As userId\n" +
            "    FROM join_group a LEFT JOIN app_user b ON a.user_id = b.id  \n" +
            "  LEFT JOIN message_list m on m.from_id=  a.group_id and m.to_id= a.user_id  \n" +
            "    where a.group_id = #{groupId} and a.user_id = #{userId} and a.del_flag = 0 and a.join_status=1 and m.del_flag=0")
    Map setLinkGroupId(int groupId, int userId);

    @Select("<script>  SELECT a.*,b.user_name AS userName,b.image_id AS imageId,c.group_num AS groupNum " +
            "FROM join_group a LEFT JOIN app_user b ON a.user_id = b.id " +
            "LEFT JOIN group_chat c ON a.group_id = c.id " +
            "where a.group_id = #{groupId} and a.user_id = #{userId} and a.del_flag = 0" +
            "</script>")
    boolean setByGroupIds(Integer fromId);

    /**
     * @param createUserId
     * @Author kdj
     * @Description createUserId
     * @Date 2023/7/27 20:35
     * @Return java.lang.Object
     */
    @Select("select count(1) from join_group ,group_chat where join_group.group_id=group_chat.id and  create_user_id=#{createUserId} and join_group.del_flag=0 and join_status=0")
    int getUnReadJoinGroup(int createUserId);

    /**
     * @param userId
     * @Author kdj
     * @Description 根据群id获取入群申请
     * @Date 2023/7/27 16:42
     * @Return com.zhuiyun.project.api.joingroup.entity.JoinGroup
     */
    @Select("select * from join_group where user_id=#{userId} and user_type=1")
    List<JoinGroup> getJoinGroupAuditByMe(int userId);

    /**
     * @param joinGroupModel
     * @Author kdj
     * @Description 审核  拒绝 让该用户加入群聊
     * @Date 2023/7/28 11:01
     * @Return int
     */
    @Update("update join_group set refuse_reason=#{refuseReason}  , update_time=now(),del_flag=1  , join_status=0 where user_id=#{userId} and user_type=1 and group_id= #{groupId}")
    int toNoJoin(JoinGroupModel joinGroupModel);

    /**
     * @param joinGroupModel
     * @Author kdj
     * @Description 审核 同意让该用户加入群聊
     * @Date 2023/7/28 11:01
     * @Return int
     */
//    @Update("update join_group set join_time=now()  , update_time=now(),del_flag=0  , join_status=1 where user_id=#{userId} and user_type=1 and group_id= #{groupId}")
    @Update("update join_group set join_time=now()  , update_time=now(),del_flag=0  , join_status=1 where user_id=#{userId} and group_id= #{groupId} and join_status=0")
    int toOkJoin(JoinGroupModel joinGroupModel);


    /**
     * @param userId
     * @param groupId
     * @Author kdj
     * @Description 判断该用户是否存在在该群中
     * @Date 2023/7/29 20:56
     * @Return int
     */
    @Select("select count(1) from join_group where user_id=#{userId} and group_id=#{groupId} and user_type=1 and del_flag=0 and join_status=1")
    int isIngroup(int userId, int groupId);

    /**
     * @Author 小江
     * @Description //TODO 判断用户是否禁言
     * @Date 11:34 2023/7/31
     **/
    @Select("select count(1) from join_group where user_id=#{userId} and group_id=#{groupId} and user_type=1 and del_flag=0 and join_status=1")
    int stormroup(int userId, int groupId);


    @Select("<script>  SELECT a.*,b.user_name AS userName,b.image_id AS imageId,c.group_num AS groupNum " +
            "FROM join_group a LEFT JOIN app_user b ON a.user_id = b.id " +
            "LEFT JOIN group_chat c ON a.group_id = c.id " +
            "where a.group_id = #{groupId} and a.del_flag = 0" +
            "</script>")
    List<JoinGroup> setByGroupIdse(Integer fromId, Integer groupId);


    /**
     * @Author 小江
     * @Description //TODO 踢出群聊
     * @Date 8:49 2023/8/2
     **/

    @Update("update join_group set del_flag=1, join_status = 1 where user_id=#{fromId} and group_id=#{groupId} and del_flag=0 and join_status=1")
    int delNoreason(Integer fromId, String groupId);

    /**
     * @Author 小江
     * @Description //TODO 退出群聊
     * @Date 9:18 2023/8/8
     **/
    @Update("update join_group set del_flag = 1" +
            " where group_id=#{groupId} and  user_id=#{userId} and del_flag = 0")
    int groudel(Integer userId, Integer groupId);


    /**
     * @Author 小江
     * @Description //TODO 退出群聊 踢出群聊
     * @Date 9:18 2023/8/8
     **/
    @Delete("delete  from join_group where group_id=#{groupId} and  user_id=#{userId} and del_flag = 0 and user_type = 1 ")
    int delGroup(Integer userId, Integer groupId);

}
